<template>
  <div class="potentialCustomers">
    <page-header :title="'售后回访'"></page-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1rem">
          <el-col :span="8">
            <el-form-item label="交易名称">
              <el-input clearable type="text" v-model="queryForm.tranName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="完成回访">
              <el-select v-model="queryForm.visitFlag" clearable placeholder="请选择" style="width:100%">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
        <el-col :span="24">
          <div class="btn_area">
            <el-button class="confrim" @click="queryList">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confrim" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="list" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle"></table-list>
    <el-dialog :title="code == '1' ? '新增售后回访记录': '编辑售后回访记录'" :visible.sync="visible" @close="close" width="60%">
      <el-row>
        <el-form :form="visitForm" label-width="1.5rem">
          <el-col :span="11">
            <el-form-item label="交易名称" required>
              <el-select :disabled="code != '1'" v-model="visitForm.tranId" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in tran" :label="item.tranName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="是否完成回访" required>
              <el-radio v-model="visitForm.visitFlag" label="1">是</el-radio>
              <el-radio v-model="visitForm.visitFlag" label="0">否</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="回访纪要" required>
              <el-input type="textarea" v-model="visitForm.summary" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="客户建议" required>
              <el-input type="textarea" v-model="visitForm.suggest" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="创建人">
               <el-input disabled v-model="visitForm.createByName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="创建时间">
              <el-input disabled v-model="visitForm.createTime"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="更新人">
              <el-input disabled v-model="visitForm.updateByName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="更新时间">
              <el-input disabled v-model="visitForm.updateTime"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confrim" @click="confrim">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import pageHeader from '@/components/pageHeader.vue'
export default {
  name: 'potentialCustomers',
  components: {
    tableList,
    pageHeader
  },
  data() {
    return {
      queryForm: {
        visitFlag: '',
        tranName: ''
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'tranName',
          label: '交易名称',
          align: 'center'
        },
        {
          prop: 'createByName',
          label: '创建人',
          align: 'center'
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: [
            {type: 'update',desc:'编辑'}
          ]
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      total: 100,
      code: '',
      visible: false,
      tran: '',
      visitForm: {
        adminId: "",
        createBy: "",
        createByName: "",
        createTime: "",
        suggest: "",
        summary: "",
        tranId: "",
        updateBy: "",
        updateByName: "",
        updateTime: "",
        uuid: "",
        visitFlag: "1"
      },
      userData: {}
    }
  },
  methods: {
    close() {
      this.visitForm = {
        adminId: "",
        createBy: "",
        createByName: "",
        createTime: "",
        suggest: "",
        summary: "",
        tranId: "",
        updateBy: "",
        updateByName: "",
        updateTime: "",
        uuid: "",
        visitFlag: ""
      }
    },
    confrim() {
      let url = this.code == '1' ? this.$api.visit.create : this.$api.visit.update
      console.log(this.visitForm);
      this.$post(url,this.reqData(this.visitForm)).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message);
          this.visible = false;
          this.queryList()
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    add() {
      this.code = '1';
      this.visible = true;
      this.visitForm.adminId = this.userData.uuid;
      this.visitForm.createBy = this.userData.uuid;
      this.visitForm.createTime = this.$time();
    },
    onHandle(data) {
      let {row,type} = data;
      this.visible = true;
      this.code = '2';
      this.$post(this.$api.visit.info,this.reqData(row.uuid)).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data;
          this.visitForm = data
          this.visitForm.adminId = this.userData.uuid;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    reset() {
      this.queryForm.visitFlag = "";
      this.queryForm.tranName = ""; 
    },
    queryList() {
      let url = this.$api.visit.list;
      this.$post(url,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data
          this.list = data.list;
          this.list.forEach((item,index) => {
            item.rowNum = (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1
          })
          this.total = data.total;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.queryList();
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.queryList();
    },
    qyeryTran() {
      this.$post(this.$api.tran.list,this.reqData({
        tranName: '',
        customerName: '',
        contact : '',
        stage: ''
      },1,9999)).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data
          this.tran = data.list.filter(item => item.stageName == "成交")
          console.log(this.tran);
        }
      })
    }
  },
  mounted() {
    this.qyeryTran();
    this.userData = JSON.parse(sessionStorage.getItem('userData'))
    this.queryList();
  }
}
</script>
<style scoped>
.potentialCustomers {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
}
</style>